package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.service.BusinessService;
import com.zkjl.protect_business.service.DocumentService;
import com.zkjl.protect_business.service.StatisticService;
import com.zkjl.protect_business.vo.statistic.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/statistics")
@Api(tags = {"统计"})
public class StatisticController {

    @Autowired
    StatisticService statisticService;
    @Autowired
    BusinessService businessService;
    @Autowired
    DocumentService documentService;


    @ApiOperation(value = "敌情社情统计", notes = "敌情社情统计", httpMethod = "GET")
    @GetMapping(value = "enemySocietys")
    @SystemLog(description = "查看敌情社情统计")
    public ApiResponse<List<EnemySocietyGroupVO>> getEnemySocietyStat() {
        List<EnemySocietyGroupVO> enemySocietyStat = statisticService.getEnemySocietyStat();
        return ApiResponse.ofSuccess(enemySocietyStat);

    }

    @ApiOperation(value = "业务分组统计", notes = "首页大屏左侧统计", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "查询单位id 不传就默认当前登录用户所属单位", required = false, paramType = "query")
    })
    @GetMapping(value = "business")
    public ApiResponse<List<BusinessStatVO>> getBusinessStat(String orgId, Long startTime, Long endTime) {

        List<BusinessStatVO> businessStat = statisticService.getBusinessStat(orgId, startTime, endTime);
        return ApiResponse.ofSuccess(businessStat);
    }


    @ApiOperation(value = "定制业务分组统计", notes = "首页大屏右侧统计", httpMethod = "GET")
    @GetMapping(value = "customBusiness")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "查询单位id 不传就默认当前登录用户所属单位", required = false, paramType = "query")
    })
    public ApiResponse<List<BusinessStatVO>> getCustomBusinessStat(Long startTime, Long endTime, String orgId) {

        List<BusinessStatVO> businessStat = statisticService.getCustomBusinessStat(startTime, endTime, orgId);
        return ApiResponse.ofSuccess(businessStat);
    }



    @ApiOperation(value = "机构信息分组", notes = "首页地图和右边单位信息一起使用", httpMethod = "GET")
    @GetMapping(value = "orgs")
    public ApiResponse<List<OrgStatVO>> getOrgStat() {

        List<OrgStatVO> orgStat = statisticService.getOrgStat();
        return ApiResponse.ofSuccess(orgStat);

    }


    @ApiOperation(value = "时间分组", notes = "首页下面折线图使用", httpMethod = "GET")
    @GetMapping(value = "dates")
    @Deprecated
    public ApiResponse<List<BusinessStatVO>> getDateGroup(Long startTime, Long endTime) {

        List<BusinessStatVO> dateGroup = statisticService.getDateGroup(startTime, endTime);
        return ApiResponse.ofSuccess(dateGroup);
    }



    @ApiOperation(value = "业务下单位文档数量折线图统计", notes = "点击首页左边的业务分类弹出框后的折线图使用", httpMethod = "GET")
    @GetMapping(value = "orgs/dateGroup")
    public ApiResponse<List<OrgStatDateVO>> getOrgDateNum(String businessId,Long startTime, Long endTime) {
        List<OrgStatDateVO> orgDateNum = statisticService.getOrgDateNum(businessId, startTime, endTime);
        return ApiResponse.ofSuccess(orgDateNum);

    }

    @ApiOperation(value = "业务下单位分组统计", notes = "点击首页左边的业务分类弹出框后的饼图和柱状图图使用", httpMethod = "GET")
    @GetMapping(value = "orgs/group")
    public ApiResponse<List<GroupVO>> getOrgGroup(String businessId,Long startTime, Long endTime) {
        List<GroupVO> orgGroup = statisticService.getOrgGroup(businessId, startTime, endTime);
        return ApiResponse.ofSuccess(orgGroup);
    }

    @ApiOperation(value = "获取二级业务分组", notes = "点击首页左边的政治考核或者敌情社情弹出框后的饼图和柱状图图使用", httpMethod = "GET")
    @GetMapping(value = "business/{businessId}/group")
    public  ApiResponse<List<GroupVO>> getAssessmentPoliticalGroup(String businessId) {

        List<GroupVO> assessmentPoliticalGroup = statisticService.getSecondaryGroup(businessId);
        return ApiResponse.ofSuccess(assessmentPoliticalGroup);
    }


    @ApiOperation(value = "获取二级业务时间分组统计", notes = "点击首页左边的政治考核弹出框后的折线图使用", httpMethod = "GET")
    @GetMapping(value = "business/{businessId}/dateGroup")
    public ApiResponse<List<OrgStatDateVO>> getAssessmentPoliticalDateGroup(String businessId) {

        List<OrgStatDateVO> secondaryDateGroup = statisticService.getSecondaryDateGroup(businessId);
        return ApiResponse.ofSuccess(secondaryDateGroup);
    }

    @ApiOperation(value = "首页数量统计", notes = "首页数量统计", httpMethod = "GET")
    @GetMapping(value = "homePage/count")
    public ApiResponse<HomePageCountVO> getHomePageCount() {

        HomePageCountVO homePageCount = statisticService.getHomePageCount();
        return ApiResponse.ofSuccess(homePageCount);
    }

    @ApiOperation(value = "获取单个业务下面的单独字段统计", notes = "根据时间分组 ", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "timeType", value = "统计维度 天day 月month", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fieldName", value = "需要统计的字段名", required = true, paramType = "path"),
            @ApiImplicitParam(name = "businessId", value = "业务id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgName", value = "单位名称", required = true, paramType = "query")
    })
    @GetMapping(value = "business/{businessId}/{fieldName}/stat")
    public ApiResponse<StatGroupDTO> getFieldNameDate(Long startTime, Long endTime, String orgName, String timeType, @PathVariable String businessId, @PathVariable String fieldName) {
        StatGroupDTO fieldNameDate = statisticService.getFieldNameDate(orgName, businessId, startTime, endTime, fieldName, timeType);
        return ApiResponse.ofSuccess(fieldNameDate);
    }

    @ApiOperation(value = "获取指定业务按照时间维度统计", notes = "获取指定业务按照时间维度统计 ", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "timeType", value = "统计维度 天day 月month 年year", required = true, paramType = "query"),
            @ApiImplicitParam(name = "businessId", value = "业务id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgName", value = "单位名称", required = true, paramType = "query")
    })
    @GetMapping(value = "business/{businessId}/date")
    public ApiResponse<List<GroupVO>> getSpecifiedGroupByIndex(Long startTime, Long endTime, String orgName, String timeType, @PathVariable String businessId) {
        List<GroupVO> specifiedGroupByIndex = documentService.getSpecifiedGroupByIndex(businessId, startTime, endTime, orgName, timeType);
        return ApiResponse.ofSuccess(specifiedGroupByIndex);
    }

    @ApiOperation(value = "获取业务下面的单独字段统计", notes = "只有分组统计", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fieldName", value = "需要统计的字段名", required = true, paramType = "path"),
            @ApiImplicitParam(name = "businessId", value = "业务id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "orgId", value = "查询单位id 不传就默认当前登录用户所属单位", required = false, paramType = "query"),
            @ApiImplicitParam(name = "fieldName", value = "字段名称", required = true, paramType = "query")
    })
    @GetMapping(value = "business/{businessId}/{fieldName}/group")
    ApiResponse<List<GroupVO>> getGroup(@PathVariable String businessId, @PathVariable String fieldName, Long startTime, Long endTime, String orgId) {

        List<GroupVO> group = businessService.getGroup(businessId, fieldName, startTime, endTime, orgId);
        return ApiResponse.ofSuccess(group);
    }

}
